#!/bin/sh

if [[ $EUID -eq 0 ]]; then

## Add to paths.d
echo "$2/bin" > /etc/paths.d/quarto

## Add symlink
ln -fs $2/bin/quarto /usr/local/bin/quarto
ln -fs $2/share/man/quarto.man /usr/local/man/quarto.1

else

# write path to zshrc
touch "${HOME}/.zshrc"

BINDIR=$2/bin
EXPORTLINE='# Add quarto to the path
if [[ -d '$BINDIR' ]]; then
  export PATH="'$BINDIR':$PATH"
fi'
# Add the path to the zshrc if it doesn't already exist
grep -qxF "$EXPORTLINE" "${HOME}/.zshrc" || echo "$EXPORTLINE" >> "${HOME}/.zshrc"

# try creating symlink, its ok if this doesn't work
ln -fs $2/bin/quarto /usr/local/bin/quarto
ln -fs $2/share/man/quarto.man /usr/local/man/quarto.1

fi

## Detect the current architecture and create symlink
FULLARCH="$(/usr/sbin/sysctl machdep.cpu.brand_string)"
  if [[ $FULLARCH == *"Intel"* ]]; then
  ARCH_DIR=x86_64
else
  ARCH_DIR=aarch64
fi
ln -fs $2/bin/tools/${ARCH_DIR}/pandoc $2/bin/tools/pandoc

exit 0

